package com.cci.kangdao.service.impl;

import com.cci.kangdao.aop.UserThreadLocal;
import com.cci.kangdao.dao.SMNTDao;
import com.cci.kangdao.dao.model.UserT;
import com.cci.kangdao.service.SMNTService;
import com.cci.kangdao.utilTool.JsonUtils;
import com.cci.kangdao.utilTool.ParameterTool;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.MapUtils;
import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("SMNTService")
public class SMNTServiceImpl implements SMNTService {

    private Logger log = Logger.getLogger(this.getClass().getName());

    @Resource
    private SMNTDao smntDao;

    @Transactional
    @Override
    public ModelAndView getUserCollectSMNList(Map<String, Object> map) {
        HttpServletResponse response = (HttpServletResponse) map.get("response");
        HttpServletRequest request = (HttpServletRequest) map.get("request");
        try {
            JSONObject obj = new JSONObject();   //保存返给手机端的数据容器
            UserT userT = UserThreadLocal.get();
            List<Map<String, Object>> collectSMNList = smntDao.getUserCollectSMNListByUserId(userT.getId());
            Map<String, Object> returnMap = new HashMap<>();
            returnMap.put("collectSMNList", collectSMNList);
            obj.put("status", "0");
            obj.put("msg", "success");
            obj.put("data", returnMap);
            ParameterTool.writeResponse(response, obj.toString());
        } catch (Exception e) {
            ParameterTool.writeErrorResponse(response);
            //主动回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            log.error(e.getMessage(), e);
        }
        return null;
    }

    @Override
    public ModelAndView getSMNList(Map<String, Object> map) {
        HttpServletResponse response = (HttpServletResponse) map.get("response");
        try {
            String smn = MapUtils.getString(map, "SMN");
            Integer page = MapUtils.getInteger(map, "page", 1);
            Integer pageSize = MapUtils.getInteger(map, "pageSize", 10);
            PageHelper.startPage(page, pageSize);
            List<Map<String, Object>> smnList = smntDao.getSMNList(smn);
            PageInfo pageInfo = new PageInfo(smnList);
            JSONObject obj = new JSONObject();
            obj.put("status", "0");
            obj.put("msg", "success");
            obj.put("data", net.sf.json.JSONObject.fromObject(pageInfo));
            ParameterTool.writeResponse(response, obj.toString());
        } catch (Exception e) {
            ParameterTool.writeErrorResponse(response);
            log.error(e.getMessage(), e);
        }
        return null;
    }
}
